package com.example.purespringboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Description: 简要功能描述
 * @Author Yabin Bai
 * @Date: 2022/1/8
 * @Version: V1.0
 */

@RestController
@Slf4j
public class FileController {

    @PostMapping("updateFile")
    public String uploadByJarDeploy(MultipartFile file, String direction) throws IOException {
        if (!StringUtils.hasText(direction)) {
            throw new RuntimeException("direction is null");
        }
        //文件名
        String originalFilename = file.getOriginalFilename();
        log.debug("文件名: {}", originalFilename);
        log.debug("文件大小: {}", file.getSize());
        log.debug("文件类型: {}", file.getContentType());

        String substring = UUID.randomUUID().toString();
        substring = substring.substring(0, 3);


        if (!StringUtils.hasText(originalFilename)) {
            originalFilename = substring;
        }
        originalFilename = substring + originalFilename;
        //上传文件到哪
        file.transferTo(new File(direction, originalFilename));

        return "success";
    }

}
